﻿function PagingHandler(options) {
	var _this = this;
	this.grid = null;
	this.previousPage = null;
	this.page = 0;
	this.pageCount = 0;
	this.pageSize = 0;
	this.options = options;
	this.pagingBar = null;
	
	this.go = function(page, callback) {
		if (page == 'next') {
			page = _this.page == _this.pageCount ? _this.page: _this.page + 1
		} else if (page == 'previous') {
			page = _this.page == 0 ? 0: _this.page-1;
		}
		var r = page * _this.pageSize;
		
		_this.grid.load(r, r + _this.pageSize, function(data) {
			_this.page = page;
			$(_this.pagingBar.find('.pages').find('a')[_this.previousPage]).removeClass('selected');
			$(_this.pagingBar.find('.pages').find('a')[_this.page]).addClass('selected');
			
			_this.previousPage = page;
			
			_this.pageCount = (_this.grid.loader.count - (_this.grid.loader.count % _this.pageSize)) / _this.pageSize;
			
			_this.pagingBar.find('.first').attr('disabled', page == 0);
			_this.pagingBar.find('.previous').attr('disabled', page == 0);
			_this.pagingBar.find('.next').attr('disabled', page == _this.pageCount-1);
			_this.pagingBar.find('.last').attr('disabled', page == _this.pageCount-1);
			
			
			var size = _this.options.scrollerSize ? _this.options.scrollerSize: 10;
			var pages = _this.pagingBar.find('.paging-bar-pages div');
			pages.empty();
			for (var i = _this.page - size; i < _this.page + size+1;i++) {
				if (0 <= i && i < _this.pageCount) {
					var b = $('<a>'+(i < 9 ? '&nbsp;': '')+(i + 1) + '</a>').attr('page', i).click(function() {
						_this.go(parseInt($(this).attr('page')));
					});
					
					if (_this.page == i) b.addClass('selected ui-state-active');
				
					pages.append(b);
				}
			}
			_this.pagingBar.find('.page-number').html(_this.page+1);
			_this.pagingBar.find('.page-count').html(_this.pageCount);
			
			if (callback != null) callback(data);
		});	
	};
	
	this.setPageSize = function(pageSize) {
		var curPage = Math.floor(_this.pageSize * _this.page / pageSize);
		this.pageSize = pageSize;
		this.go(curPage);
	};
	
	this.createPagingBar = function() {
		var apagingBar = $('<table>').addClass('paging-bar');
		var pagebar = $('<td>').addClass('paging-bar-item paging-bar-pages');
		pagebar.append($('<div>'));
		
		var navbar = $('<td>').addClass('paging-bar-item paging-bar-nav').css('vertical-align', 'center');
		navbar.append($('<a>').addClass('ui-icon first ui-icon-seek-first').css('display', 'inline-block').click(function() {_this.go(0);}));
		navbar.append($('<a>').addClass('ui-icon previous ui-icon-triangle-1-w').css('display', 'inline-block').click(function() {_this.go('previous');}));
		navbar.append($('<a>').attr('href', 'javascript:none').append(
			$('<span>').addClass('page-number'), ' of ', $('<span>').addClass('page-count')
		));
		
		/*inp.bind('keydown blur', function(e) {
			if ( (e.type == 'keydown'  && e.keyCode == 13) || e.type == 'blur') {
				_this.go(parseInt(this.value)-1);
			}
		});*/
		navbar.append($('<a>').addClass('ui-icon next ui-icon-triangle-1-e').css('display', 'inline-block').click(function() {_this.go('next');}));
		navbar.append($('<a>').addClass('ui-icon last ui-icon-seek-end').css('display', 'inline-block').click(function() {_this.go(_this.pageCount-1);}));
		
		var pagesizebar = $('<td>').addClass('paging-bar-item paging-bar-page-size');
		pagesizebar.append($('<span>').html('Items per page:'));
		var pageSizeSelect = $('<select>');
		if (options.pageSizes==null) options.pageSizes = [10,20,50];
		for (var i = 0; i < options.pageSizes.length;i++) {
			pageSizeSelect.append($('<option>').attr('selected', options.pageSizes[i] == options.pageSize).attr('value', options.pageSizes[i]).html(options.pageSizes[i]));
		}
		pageSizeSelect.change(function() {
			_this.setPageSize(parseInt(this.value));
		});
		
		pagesizebar.append(pageSizeSelect);
		
		var tr = $('<tr>').append(navbar, pagebar, pagesizebar);
		apagingBar.append(tr);
		return apagingBar;
	};
	
	this.attach = function(grid) {
		this.grid = grid;
		this.pageSize = options.pageSize;
		this.pagingBar = this.createPagingBar();
		this.grid.getFooter().append(_this.pagingBar);
		if (this.grid.isAttached) {
			_this.go(0);
		} else {
			this.grid.attached.add(function() {
				_this.go(0);
			});
		}
	};
	
	this.detach = function() {
		this.grid.clear();
		this.pagingBar.remove();
	};
}; PagingHandler.inherits(DatagridPlugin);